package mybbs;

import java.io.*;
//import com.mysql.jdbc.Driver;
import java.sql.*;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class search_result extends HttpServlet {

  PrintWriter out;
  ResultSet rs;
  String sql;
  String action,userid;
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException
    {
        response.setContentType("text/html;charset=gbk");
        out = response.getWriter();
        try{
        DBConn conn=new DBConn();
        CAEncode cod=new CAEncode();
        String nickname   =cod.htmlEncode(request.getParameter("nickname"));
        String keyword =cod.htmlEncode(request.getParameter("keyword"));
        String keyword2 =cod.htmlEncode(request.getParameter("keyword2"));
        String forumid=cod.htmlEncode(request.getParameter("forumsid"));
        if(request.getParameter("nickname")==null||nickname.equals("")){
             nickname="%";}

          if(request.getParameter("keyword")==null||keyword.equals("")){
             keyword="_*";}
              if(request.getParameter("keyword2")==null||keyword2.equals("")){
             keyword2="_*";}
          if(!forumid.equals("")){
             forumid="="+forumid;
            }
          if(request.getParameter("forumsid")==null||forumid.equals("")){
             forumid=">0";}

      sql="SELECT * FROM posts WHERE isdel=0 AND author LIKE '"+nickname+"' AND content REGEXP '"+keyword2+"' AND title REGEXP '"+keyword+"' AND forumid "+forumid;
          System.out.print(sql);
        rs=conn.query(sql);
        out.print("<TABLE <TABLE class=i_table cellSpacing=1 cellPadding=5 width='98%' align=center><TBODY>");
        out.print("<TR><TD class=head height=24 align=left colSpan=2 background='images/head.gif'></TD></TR>");
        int i=1;
        while(rs.next()){
        out.print("<TR><TD height='23' align=left class=f_one onmouseover=this.className='f_two' onmouseout=this.className='f_one'>");
        out.print(i+"&nbsp;&nbsp;&nbsp;|");
        if((rs.getString("title")).equals("")){
           String con;
         if(rs.getString("content").length()>50)
           con=rs.getString("content").substring(0,50);
          else
            con=rs.getString("content");
         out.print("<A href='mydisbbs.do?forumsid="+rs.getString("forumid")+"&topicid="+rs.getString("topic_id")+"'>"+cod.htmlEncode(con)+"</A>|&nbsp;");
         }
        else{
        out.print("<A href='mydisbbs.do?forumsid="+rs.getString("forumid")+"&topicid="+rs.getString("topic_id")+"'>"+rs.getString("title")+"</A>|&nbsp;");
           }
        out.print("<A href='user_info.do?userid="+rs.getString("author_id")+"'><FONT color=blue>"+rs.getString("author")+"</FONT></A>|&nbsp;");

        out.print("</TD></TR>");
        i++;
        }
       out.print("</TBODY></TABLE>");
      rs.close();
      conn.DBclose();
      }
     catch(Exception e){
        request.setAttribute("throw",e.toString());
        response.sendRedirect("global.do");
        }
    }

 public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException
    {
        doPost(request, response);
    }
}
